package com.lijing.controller;

import com.lijing.model.User;
import com.lijing.service.UserService;
import com.lijing.util.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/userRegister", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String register(User user){
        user.setType("社员");
        userService.register(user);
        System.out.println(user.getUname());
        return JSONResponse.getOK("注册成功");
    }

    @RequestMapping(value = "/userUpdate", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String update(User user){
        System.out.println(user.getUname());
        System.out.println(user.getUid());
        System.out.println(user.getClub());
        userService.update(user);
        return "{success:\"OK\",  msg:\"修改成功\"}";
    }

    @ResponseBody
    @RequestMapping("/getUser")
    public User getUser(Integer uid){
        User user  = userService.getUser(uid);
        return user;
    }

    @ResponseBody
    @RequestMapping("/getUserByUsername")
    public User getUserByUsername(String username){
        User user  = userService.getUserByUsername(username);
        return user;
    }

    @RequestMapping(value = "/userLogin")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, ModelMap mop, HttpSession session){
        User user = userService.login(username, password);
        if (user == null){
            mop.addAttribute("status","用户名或密码错误！");
            return "login";
        }
        else if (user.getType().equals("社员")){
            session.setAttribute("user", user);
            return "index";
        }else if (user.getType().equals("社长")){
            session.setAttribute("user", user);
            return "index";
        }else if (user.getType().equals("管理员")){
            session.setAttribute("user", user);
            return "index";
        }else {
            return "index";
        }
    }

    @GetMapping(value = "/logout")
    @ResponseBody
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "你已成功退出！";
    }

    @RequestMapping(value = "/updatePassword", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String updatePassword(@RequestParam("password") String password, @RequestParam("password1") String password1, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (password.equals(user.getPassword())){
            user.setPassword(password1);
            userService.update(user);
            return "{success:\"OK\", msg:\"修改密码成功\"}";
        }else {
            return "{error:1, msg:\"密码错误\"}";
        }
    }
}
